//
//  BookShelfViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/15.
//  Copyright © 2018年 cxd. All rights reserved.
//  书架

#import "BookShelfViewController.h"
#import "TopTitleView.h"
#import "LibraryViewController.h"
#import "VoiceBookViewController.h"

@interface BookShelfViewController () <UIScrollViewDelegate>

@property (nonatomic, strong)TopTitleView *topView;
@property (nonatomic, strong)UIScrollView *bookScrollView;
@property (nonatomic, strong)NSArray *nameArr;

@end

@implementation BookShelfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self buildUI];
}

- (void)buildUI {
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bookScrollView];
    [self setScrollViewContent];
    __weak typeof(self) WeakSelf = self;
    self.topView.clickBtnBlock = ^(NSInteger tag) {
        [UIView animateWithDuration:0.25 animations:^{
            WeakSelf.bookScrollView.contentOffset = CGPointMake(kScreenWidth * (tag - 1), 0);
        }];
    };
}

- (void)setScrollViewContent {
    NSArray *tempArr = @[@"LibraryViewController",@"VoiceBookViewController"];
    self.bookScrollView.contentSize = CGSizeMake(tempArr.count * kScreenWidth, 0);
    for (NSInteger i = 0; i < self.nameArr.count; i++) {
        Class cls = NSClassFromString(tempArr[i]);
        UIViewController *viewController = [[cls alloc] init];
        viewController.view.frame = CGRectMake(i * kScreenWidth, 0, kScreenWidth, self.bookScrollView.height);
        [self addChildViewController:viewController];
        [self.bookScrollView addSubview:viewController.view];
    }
}
#pragma mark - 懒加载
- (TopTitleView *)topView {
    if (!_topView) {
        _topView = [[TopTitleView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
        _topView.titleArray = self.nameArr;
    }
    return _topView;
}

- (UIScrollView *)bookScrollView {
    if (!_bookScrollView) {
        _bookScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.topView.bottom, kScreenWidth, kScreenHeight - 64 - 49 - 44)];
        _bookScrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
        _bookScrollView.showsVerticalScrollIndicator = NO;
        _bookScrollView.showsHorizontalScrollIndicator = NO;
        _bookScrollView.pagingEnabled = YES;
        _bookScrollView.delegate = self;
    }
    return _bookScrollView;
}

- (NSArray *)nameArr {
    if (!_nameArr) {
        _nameArr = [NSArray arrayWithObjects:@"图书",@"听书", nil];
    }
    return _nameArr;
}
#pragma mark - 协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x < 0 || scrollView.contentOffset.x > kScreenWidth) {
        return;
    }
    self.topView.tipView.transform = CGAffineTransformMakeTranslation(scrollView.contentOffset.x / self.nameArr.count, 0);
    NSUInteger tagNum = (scrollView.contentOffset.x + kScreenWidth * 0.5) / kScreenWidth + 1;
    [self.topView changeBtnColor:tagNum];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger tagNum = (scrollView.contentOffset.x + kScreenWidth * 0.5) / kScreenWidth + 1;
    [self.topView changeBtnColor:tagNum];
}

@end
